/*****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
 * under the License.
 ****************************************************************/
package reco.db.validation;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Maps.newHashMap;

import java.util.Map;

import reco.db.Domain;
import reco.db.Field;

/**
 * 
 */
public class DomainErrors {

    private final String domainName;
    private final Map<String, FieldErrors> errorsByField;

    public DomainErrors(Domain<?> domain) {
        this(checkNotNull(domain, "domain").getClass().getSimpleName());
    }

    public DomainErrors(String domainName) {
        this.domainName = checkNotNull(domainName, "domainName");
        this.errorsByField = newHashMap();
    }

    /**
     * Return the name of the bound domain object.
     */
    public String getDomainName() {
        return this.domainName;
    }

    public void registerErrorOn(Field<?, ?> field, FieldError fieldError) {
        checkNotNull(field, "field");
        checkNotNull(fieldError, "fieldError");

        FieldErrors errors = this.errorsByField.get(field.getName());

        if (errors == null) {
            errors = new FieldErrors();
            this.errorsByField.put(field.getName(), errors);
        }

        errors.register(fieldError);
    }

    public FieldErrors errorsOf(Field<?, ?> field) {
        FieldErrors errors = this.errorsByField.get(checkNotNull(field, "field").getName());

        if (errors == null) {
            errors = new FieldErrors();
            this.errorsByField.put(field.getName(), errors);
        }

        return errors;
    }

    /**
     * Return if there were any errors.
     */
    public boolean hasErrors() {
        for (final FieldErrors fieldErrors : this.errorsByField.values()) {
            if (fieldErrors.hasErrors()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Return the total number of errors.
     */
    public int getErrorCount() {
        int errorCount = 0;
        for (final FieldErrors fieldErrors : this.errorsByField.values()) {
            errorCount += fieldErrors.getErrorCount();
        }
        return errorCount;
    }

    public void discardErrors() {
        for (final FieldErrors fieldErrors : this.errorsByField.values()) {
            fieldErrors.discardErrors();
        }
    }

}
